package waskung

import (
	"database/sql"
	"fmt"
	"strconv"
	"time"
)

// Action sets t's action attribute.
func (t *TagBrush) Action(val string) *TagBrush {
	return t.Attr("action", val)
}

// Autocomplete sets t's autocomplete attribute.
func (t *TagBrush) Autocomplete(b bool) *TagBrush {
	v := "off"
	if b {
		v = "on"
	}
	return t.Attr("autocomplete", v)
}

// Autofocus sets t's autofocus attribute if b is true.
func (t *TagBrush) Autofocus(b bool) *TagBrush {
	if b {
		t.Attr("autofocus", "")
	}
	return t
}

// Class sets t's class attribute.
func (t *TagBrush) Class(val string) *TagBrush {
	return t.Attr("class", val)
}

// Classes sets t's class attribute to those keys in m whose value is true.
func (t *TagBrush) Classes(m map[string]bool) *TagBrush {
	val := ""
	for k, v := range m {
		if v {
			if val != "" {
				val += " "
			}
			val += k
		}
	}
	if val != "" {
		t.Attr("class", val)
	}
	return t
}

// Cols sets t's cols attribute.
func (t *TagBrush) Cols(val int) *TagBrush {
	return t.Attr("cols", strconv.Itoa(val))
}

// Colspan sets t's colspan attribute.
func (t *TagBrush) Colspan(val int) *TagBrush {
	return t.Attr("colspan", strconv.Itoa(val))
}

// Content sets t's content attribute.
func (t *TagBrush) Content(val string) *TagBrush {
	return t.Attr("content", val)
}

// Hidden sets t's hidden attribute if b is true.
func (t *TagBrush) Hidden(b bool) *TagBrush {
	if b {
		t.Attr("hidden", "")
	}
	return t
}

// ID sets t's id attribute.
func (t *TagBrush) ID(val string) *TagBrush {
	return t.Attr("id", val)
}

// Method sets t's method attribute.
func (t *TagBrush) Method(val string) *TagBrush {
	return t.Attr("method", val)
}

// Name sets t's name attribute.
func (t *TagBrush) Name(val string) *TagBrush {
	return t.Attr("name", val)
}

// OnChange sets t's onchange attribute.
func (t *TagBrush) OnChange(val string) *TagBrush {
	return t.Attr("onchange", val)
}

// OnClick sets t's onclick attribute.
func (t *TagBrush) OnClick(val string) *TagBrush {
	return t.Attr("onclick", val)
}

// Placeholder sets t's placeholder attribute.
func (t *TagBrush) Placeholder(val string) *TagBrush {
	return t.Attr("placeholder", val)
}

// Rows sets t's rows attribute.
func (t *TagBrush) Rows(val int) *TagBrush {
	return t.Attr("rows", strconv.Itoa(val))
}

// Rowspan sets t's rowspan attribute.
func (t *TagBrush) Rowspan(val int) *TagBrush {
	return t.Attr("rowspan", strconv.Itoa(val))
}

// Size sets t's size attribute.
func (t *TagBrush) Size(val int) *TagBrush {
	return t.Attr("size", strconv.Itoa(val))
}

// Style sets t's style attribute.
func (t *TagBrush) Style(val string) *TagBrush {
	return t.Attr("style", val)
}

// Title sets t's title attribute (tooltip).
func (t *TagBrush) Title(val string) *TagBrush {
	return t.Attr("title", val)
}

// Type sets t's type attribute.
func (t *TagBrush) Type(val string) *TagBrush {
	return t.Attr("type", val)
}

// Value sets t's value attribute.
func (t *TagBrush) Value(val interface{}) *TagBrush {
	switch v := val.(type) {
	case string:
		t.Attr("value", v)
	case sql.NullString:
		if v.Valid {
			t.Attr("value", v.String)
		}
	case sql.NullInt64:
		if v.Valid {
			t.Attr("value", strconv.FormatInt(v.Int64, 10))
		}
	case time.Time:
		if !v.IsZero() {
			t.Attr("value", v.Format("2006-01-02"))
		}
	default:
		t.Attr("value", fmt.Sprint(v))
	}
	return t
}
